package com.upc.cajabancos.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.OracleTypes;

import com.upc.cajabancos.model.Banco;
import com.upc.cajabancos.util.DataAccessObjetc;

public class BancoDAO {
	
	private Connection connection = null;
	
	public BancoDAO(Connection connection){
		this.connection = connection;
	}
	
	/**
	 * Lista los bancos
	 * @author Cesar Jesus
	 * @since 30/01/2011
	 * @return List<Banco>
	 */
	public List<Banco> listar() throws Exception{
		CallableStatement cstmt = null;	
		ResultSet rs = null;
		Banco banco = null;
		List<Banco> listaBancos = new ArrayList<Banco>();
    	
    	try{
			
			cstmt = connection.prepareCall(DataAccessObjetc.buildProcedureCall("LISTAR_BANCOS",1));
			
			cstmt.registerOutParameter(1, OracleTypes.CURSOR); //REF CURSOR
			cstmt.execute();
			rs = (OracleResultSet)cstmt.getObject(1);
			
			while (rs.next()){
				banco = new Banco();
				banco.setCodigo(rs.getString("Co_codigo"));
				banco.setNombre(rs.getString("Tx_Descripcion"));

				listaBancos.add(banco);
			}
			
    	}
    	catch(Exception e){
    		throw e;
    	}
    	finally {
    		DataAccessObjetc.close(null,cstmt,rs);
		}
    	
    	return listaBancos;
    }

}
